﻿


//声明了个控制权名称：common.views.areas.index
(function () {
    appModule.controller('common.views.areas.upPicture', [
      '$scope', '$uibModal', '$stateParams', 'uiGridConstants', 'abp.services.app.file', 'FileUploader', '$state',
        function ($scope, $uibModal, $stateParams, uiGridConstants, fileService, fileUploader, $state) {
            //$stateParams 获取的是浏览器后面跟的参数
            var vm = this;

            vm.areaId = $stateParams.filterText;

            $scope.$on('$viewContentLoaded', function () {
                //这里应该是当页面加载完毕后，进行信息的初始化
                //实际会去调用 icheck、select2等js的初始化插件。来渲染页面
                App.initAjax();
            });
            //告知页面信息已经下载完毕
            vm.loading = false;

            //请求参数，默认用于分页
            vm.requestParams = {
                permission: '',
                role: '',
                skipCount: 0,
                //这里是个常量文件，如果你没有找到这个常量文件，那么就手动改成10吧
                maxResultCount: app.consts.grid.defaultPageSize,
                sorting: null,
                disID: vm.areaId
            };

            //返回
            vm.back = function () {
                //跳转页面
                $state.go('areas');
            };

            var uploader = $scope.uploader = new fileUploader({
                url: abp.appPath + 'Profile/UploadPicture?areaID=' + vm.areaId,
                headers: {
                    "X-XSRF-TOKEN": abp.security.antiForgery.getToken()
                }
            });

            // FILTERS

            // a sync filter
            uploader.filters.push({
                name: 'syncFilter',
                fn: function (item /*{File|FileLikeObject}*/, options) {

                    //File type check
                    var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
                    if ('|jpg|jpeg|png|gif|'.indexOf(type) === -1) {
                        abp.message.warn(app.localize('Picture_Warn_FileType'));
                        return false;
                    }

                    if (item.size > 4194304) //4MB
                    {
                        abp.message.warn(app.localize('Picture_Warn_SizeLimit'));
                        return false;
                    }


                    return this.queue.length < 10;
                }
            });

            // CALLBACKS

            uploader.onWhenAddingFileFailed = function (item /*{File|FileLikeObject}*/, filter, options) {
                console.info('onWhenAddingFileFailed', item, filter, options);
            };
            uploader.onAfterAddingFile = function (fileItem) {
                console.info('onAfterAddingFile', fileItem);
            };
            uploader.onAfterAddingAll = function (addedFileItems) {
                console.info('onAfterAddingAll', addedFileItems);
            };
            uploader.onBeforeUploadItem = function (item) {
                console.info('onBeforeUploadItem', item);
            };
            uploader.onProgressItem = function (fileItem, progress) {
                console.info('onProgressItem', fileItem, progress);
            };
            uploader.onProgressAll = function (progress) {
                console.info('onProgressAll', progress);
            };
            uploader.onSuccessItem = function (fileItem, response, status, headers) {
                console.info('onSuccessItem', fileItem, response, status, headers);
            };
            uploader.onErrorItem = function (fileItem, response, status, headers) {
                console.info('onErrorItem', fileItem, response, status, headers);
            };
            uploader.onCancelItem = function (fileItem, response, status, headers) {
                console.info('onCancelItem', fileItem, response, status, headers);
            };
            uploader.onCompleteItem = function (fileItem, response, status, headers) {
                console.info('onCompleteItem', fileItem, response, status, headers);
            };
            uploader.onCompleteAll = function () {
                console.info('onCompleteAll');
            };

            //声明查询方法
            vm.getFiles = function () {
                vm.loading = true;
                fileService.getAllFilesAsync($.extend({ filterText: vm.filterText }, vm.requestParams))
                    .then(function (result) {
                        vm.files = result.data.items;
                    }).finally(function () {
                        vm.loading = false;
                    });
            };

            //删除方法
            vm.deleteFiles = function (file) {
                abp.message.confirm(
                    app.localize('FileDeleteWarningMessage', file.title),
                    function (isConfirmed) {
                        if (isConfirmed) {
                            fileService.deleteFileAsync({
                                id: file.id
                            }).then(function () {
                                vm.getFiles();
                                abp.notify.success(app.localize('SuccessfullyDeleted'));
                            });
                        }
                    }
                );
            };

            vm.getFiles();
            console.info('uploader', uploader);

        }
    ]);
})();











